<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.stockId ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="errorForm" :rules="errorRule" ref="errorForm" @keyup.enter.native="errorFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="排" prop="row_num">
        <el-input v-model="errorForm.row_num" placeholder="排"></el-input>
      </el-form-item>
      <el-form-item label="列" prop="col_num">
        <el-input v-model="errorForm.col_num" placeholder="列"></el-input>
      </el-form-item>
      <el-form-item label="层" prop="level_num">
        <el-input v-model="errorForm.level_num" placeholder="层"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
<!--      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>-->
      <!-- 新增按钮：提交 errorForm -->
      <el-button type="primary" @click="errorFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        stockId: '',
        contNo: '',
        curPosition: '',
        spuNo: '',
        spuName: '',
        lotNo: '',
        warehouseId: '',
        stockQnty: '',
        lockedQnty: '',
        stockSt: '',
        lockSt: '',
        qualitySt: '',
        comments: ''
      },
      errorForm:{
        row_num:'',
        col_num:'',
        level_num:''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        contNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        spuNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        spuName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lotNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        stockQnty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lockedQnty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    },
    errorRule() {
      return {
        row_num: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitErrorForm() {
      this.errorFormSubmitHandle();
    },
    init() {
      this.visible = true
      this.dataForm.stockId = this.dataForm.id
      this.$nextTick(() => {
        // this.$refs['dataForm'].resetFields()
        this.$refs['errorForm'].resetFields()
        if (this.dataForm.stockId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/wms/stock/${this.dataForm.stockId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    errorFormSubmitHandle: debounce(function () {
      this.$refs['errorForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        console.log("表单提交数据" + this.errorForm)
        this.$http.post('/wms/stock/error', this.errorForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.stockId ? 'post' : 'put']('/wms/stock/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
